<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/1/8
 * Time: 10:08
 */

namespace app\api\controller;


use think\Db;

class Notice extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status' => 301, 'msg' => $this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 系统消息列表
     * @return \think\response\Json
     * page    页码
     */
    public function noticeList(){
        $list = $this->getNotice();
        if($list){
            foreach($list as $k => $v){
                if(strpos($v['notice_read_user'], ',' . $this->user_id . ',') !== false){
                    $list[$k]['is_read'] = 1; // 已读
                }else{
                    $list[$k]['is_read'] = 2; // 未读
                }
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 消息详情
     * @param $notice_id     消息ID
     * @return \think\response\Json
     */
    public function noticeDetail($notice_id){
        $detail = Db::table('up_notice')
            ->where('notice_id', $notice_id)
            ->field('notice_title, notice_content, notice_publish_user, notice_time')
            ->find();
        return json(['status' => 200, 'msg' => $detail]);
    }
    
    /**
     * 标记已读
     * @return \think\response\Json
     * notice_id     消息ID
     */
    public function readNotice(){
        $notice_id = input('notice_id');
        $notice = Db::table('up_notice')
            ->where('notice_id', $notice_id)
            ->field('notice_read_user')
            ->find();
        if(strpos($notice['notice_read_user'], ',' . $this->user_id . ',') === false){
            $read_user = $notice['notice_read_user'] . $this->user_id . ',';
            Db::table('up_notice')->where('notice_id', $notice_id)->setField('notice_read_user', $read_user);
        }
        return json(['status' => 200, 'msg' => '']);
    }
    
    /**
     * 清空消息
     * @return \think\response\Json
     */
    public function delAllNotice(){
        $notice = $this->getNotice(1, 2);
        if($notice){
            Db::startTrans();
            try{
                foreach($notice as $k => $v){
                    $del_user = $v['notice_del_user'] . $this->user_id . ',';
                    Db::table('up_notice')->where('notice_id', $v['notice_id'])->setField('notice_del_user', $del_user);
                }
                Db::commit();
                return json(['status' => 200, 'msg' => '清空消息成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['status' => 0, 'msg' => $e->getMessage()]);
            }
        }else{
            return json(['status' => 200, 'msg' => '没有需要清空的消息']);
        }
    }
    
    /**
     * 未读消息数量
     * @return \think\response\Json
     */
    public function unreadNum(){
        $num = $this->getNotice(2);
        return json(['status' => 200, 'msg' => $num]);
    }
    
    /**
     * 获取系统消息
     * @param int $type   1 列表数据    2 未读消息统计
     * @param int $del    1 其它        2 清空消息
     * @return false|int|\PDOStatement|string|\think\Collection
     */
    private function getNotice($type = 1, $del = 1){
        $where['notice_type'] = 1;

        $where['notice_del_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        if($type == 2){
            $where['notice_read_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        }
        $user = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_th_rule');
        $where1['notice_user'] = [['like', '%,' . $this->user_id . ',%'], ['=', -1], 'or'];
        $query = Db::table('up_notice')
            ->where($where)
            ->where(function ($query) use ($user, $where1){
                $query->where($where1)
                    ->whereOr(function ($query1) use($user){
                        $query1->where(['notice_range' => 3, 'notice_th_rule' => $user]);

                });
            });
        if($type == 1){
            $query->order('notice_time desc')
                ->field('notice_id, notice_title, notice_content, notice_publish_user, notice_read_user, 
                notice_time, notice_del_user, notice_range');
            if($del == 1){
                $query->page($this->page, 10);
            }
            $data = $query->select();
        }else{
            $data = $query->count();
        }
        return $data;
    }
}